package com.china08.yxyapi.api.manage.sch.platform.attendance.setting;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.attendance.YxyYxyTecAttendanceConfig;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.attendance.YxyTecAttendanceConfigRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class AttendAnceSettingUpdController extends YxyApiBaseController {

    @Autowired
    YxyTecAttendanceConfigRepository yxyTecAttendanceConfigRepository;

    @ApiOperation("设置考勤配置")
    @RequestMapping(value = "/api_yw/manage/sch/platform/attendance/setting/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) throws ParseException {

        String schoolId = getProfile(request, response).getSchoolId();

        // 学校配饰
        YxyYxyTecAttendanceConfig yxyTecAttendanceConfig = yxyTecAttendanceConfigRepository.findBySchoolId(schoolId);

        String times = reqModel.times;
        Double latitude = reqModel.latitude;
        Double longitude = reqModel.longitude;
        Integer distance = reqModel.distance;
        String gateway = reqModel.gateway;
        Date clock1 = reqModel.clock11;
        Date clock2 = reqModel.clock12;
        Date clock3 = reqModel.clock13;
        Date clock4 = reqModel.clock14;
        Date winterVacationBegin = reqModel.winterVacationBegin;
        Date winterVacationEnd = reqModel.winterVacationEnd;
        Date summerVacationBegin = reqModel.summerVacationBegin;
        Date summerVacationEnd = reqModel.summerVacationEnd;
        boolean monday = reqModel.monday;
        boolean tuesday = reqModel.tuesday;
        boolean wednesday = reqModel.wednesday;
        boolean thursday = reqModel.thursday;
        boolean friday = reqModel.friday;
        boolean saturday = reqModel.saturday;
        boolean sunday = reqModel.sunday;

        // 第一次使用
        if (null == yxyTecAttendanceConfig) {
            yxyTecAttendanceConfig = new YxyYxyTecAttendanceConfig();
            yxyTecAttendanceConfig.setSchoolId(schoolId);
        }
        yxyTecAttendanceConfig.setLatitude(latitude);
        yxyTecAttendanceConfig.setLongitude(longitude);
        yxyTecAttendanceConfig.setDistance(distance);
        yxyTecAttendanceConfig.setTimes(times);
        yxyTecAttendanceConfig.setGateway(gateway);
        yxyTecAttendanceConfig.setClock1(new Time(clock1.getTime()));
        yxyTecAttendanceConfig.setClock2(new Time(clock2.getTime()));
        if (times.equals("4")) {
            yxyTecAttendanceConfig.setClock3(new Time(clock3.getTime()));
            yxyTecAttendanceConfig.setClock4(new Time(clock4.getTime()));
        }
        if (null != winterVacationBegin && null != winterVacationEnd) {
            yxyTecAttendanceConfig.setWinterVacationBegin(winterVacationBegin);
            yxyTecAttendanceConfig.setWinterVacationEnd(winterVacationEnd);
        }
        if (null != summerVacationBegin && null != summerVacationEnd) {
            yxyTecAttendanceConfig.setSummerVacationBegin(summerVacationBegin);
            yxyTecAttendanceConfig.setSummerVacationEnd(summerVacationEnd);
        }
        yxyTecAttendanceConfig.setMonday(monday);
        yxyTecAttendanceConfig.setTuesday(tuesday);
        yxyTecAttendanceConfig.setWednesday(wednesday);
        yxyTecAttendanceConfig.setThursday(thursday);
        yxyTecAttendanceConfig.setFriday(friday);
        yxyTecAttendanceConfig.setSaturday(saturday);
        yxyTecAttendanceConfig.setSunday(sunday);
        yxyTecAttendanceConfig.setStatus(1);

        yxyTecAttendanceConfigRepository.save(yxyTecAttendanceConfig);
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String times;
        public Double latitude;
        public Double longitude;
        public Integer distance;
        public String gateway;
        public String clock1;
        public String clock2;
        public String clock3;
        public String clock4;
        public Date clock11;
        public Date clock12;
        public Date clock13;
        public Date clock14;
        public Date winterVacationBegin;
        public Date winterVacationEnd;
        public Date summerVacationBegin;
        public Date summerVacationEnd;
        public boolean monday;
        public boolean tuesday;
        public boolean wednesday;
        public boolean thursday;
        public boolean friday;
        public boolean saturday;
        public boolean sunday;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(gateway), "网关ip地址不能为空");
            Assert.isTrue(StringUtils.isNotBlank(times), "打卡次数不能为空");
            Assert.isTrue(StringUtils.isNotBlank(clock1), "第一次打卡时间不能为空");
            Assert.isTrue(StringUtils.isNotBlank(clock2), "第二次打卡时间不能为空");

            if (times.equals("4")) {
                Assert.isTrue(clock3 != null, "第三次打卡时间不能为空");
                Assert.isTrue(clock4 != null, "第四次打卡时间不能为空");
            }

            try {
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                clock11 = sdf.parse(clock1);
                clock12 = sdf.parse(clock2);
                if (times.equals("4")) {
                    clock13 = sdf.parse(clock3);
                    clock14 = sdf.parse(clock4);
                }
            } catch (Exception e) {
                Assert.isTrue(false, "时日类型错误");
            }
            Assert.isTrue(clock12.getTime() > clock11.getTime(), "第二次打卡时间不能小于第一次打卡时间");
            if (times.equals("4")) {
                Assert.isTrue(clock13.getTime() > clock12.getTime(), "第三次打卡时间不能小于第二次打卡时间");
                Assert.isTrue(clock14.getTime() > clock13.getTime(), "第四次打卡时间不能小于第三次打卡时间");
            }
            Assert.isTrue((clock12.getTime() - clock11.getTime()) >= (60 * 30 * 1000), "打卡时间间隔要在30分钟以上");
            if (times.equals("4")) {
                Assert.isTrue((clock13.getTime() - clock12.getTime()) >= (60 * 30 * 1000), "打卡时间间隔要在30分钟以上");
                Assert.isTrue((clock14.getTime() - clock13.getTime()) >= (60 * 30 * 1000), "打卡时间间隔要在30分钟以上");
            }
            if (winterVacationBegin != null || winterVacationEnd != null) {
                Assert.isTrue(winterVacationBegin != null, "寒假开始时间不能为空");
                Assert.isTrue(winterVacationEnd != null, "寒假结束时间不能为空");
                Assert.isTrue(winterVacationEnd.getTime() > winterVacationBegin.getTime(), "寒假结束时间不能小于寒假开始时间");
            }
            if (summerVacationBegin != null || summerVacationEnd != null) {
                Assert.isTrue(summerVacationBegin != null, "暑假开始时间不能为空");
                Assert.isTrue(summerVacationEnd != null, "暑假结束时间不能为空");
                Assert.isTrue(summerVacationEnd.getTime() > summerVacationBegin.getTime(), "暑假结束时间不能小于暑假开始时间");
            }
        }
    }
}
